package com.ebt.m.customer.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ebt.m.R;

import java.util.List;

/**
 * Created by archer.qi on 2016/12/7.
 */
public class ListDialog extends SweetDialog {
    private ListView listView;
    private SimpleListAdapter simpleAdapter;
    private List<String> listData;

    private Context context;
    private LayoutInflater inflater;

    /* 标识 */
    private int FLAG;
    private onMenuSelectListener listener;

    public ListDialog(Context context) {
        super(context);
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getContentResourceId() {
        return R.layout.dialog_list;
    }

    @Override
    public void init() {
        listView = (ListView) v(R.id.dialog_list_listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                if (listener != null) {
                    listener.onMenuSelect(FLAG, position, listData.get(position));
                }
            }
        });
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(List<String> data) {
        listData = data;
        if (simpleAdapter == null) {
            simpleAdapter = new SimpleListAdapter();
        }
        listView.setAdapter(simpleAdapter);
    }

    /**
     * 设置判定标识
     *
     * @param flag
     */
    public void setFlag(int flag) {
        FLAG = flag;
    }

    public int getFlag() {
        return FLAG;
    }

    public void addOnItemClickListener(onMenuSelectListener listener) {
        this.listener = listener;
    }

    private class SimpleListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return listData == null ? 0 : listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView text;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_item_simple_1, null);
            }

            text = (TextView) convertView.findViewById(R.id.list_item_simple_1_text);
            text.setText(listData.get(position));
            if (getCount() == 1) {
                text.setBackgroundResource(R.drawable.selector_list_item_single);
            } else {
                if (position == 0) {
                    text.setBackgroundResource(R.drawable.selector_list_item_top);
                } else if (position == getCount() - 1) {
                    text.setBackgroundResource(R.drawable.selector_list_item_bottom);
                } else {
                    text.setBackgroundResource(R.drawable.selector_list_item_middle);
                }
            }

            return convertView;
        }
    }

    public interface onMenuSelectListener {
        void onMenuSelect(int flag, int position, String content);
    }

}
